package com.guo.loadinganim;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.transition.TransitionManager;

public class AnimApplication extends Application {
    private static AnimApplication sInstance;
    private static final String TAG = "AnimApplication";
    private ViewTransitionManager mViewTransitionManager;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        mViewTransitionManager = new ViewTransitionManager();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                if(activity instanceof MainActivity){
                    Log.d(TAG, "onResume , flag = " + mTopBarAdded);
                    if(mTopBarAdded){
                        Log.d(TAG, "onResume remove view");
                        AnimApplication.getInstance().removeTopBarView();
                    }
                }
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                if(activity instanceof MainActivity){
                    if(!mTopBarAdded){
                        initTopBarView(activity);
                        Log.d(TAG, " re attach top bar， flag = " + mTopBarAdded);
                    }
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    public static AnimApplication getInstance(){
        return sInstance;
    }


    private LinearLayout linearLayout;
    private WindowManager wm;
    public boolean mTopBarAdded = false;
    private Bundle mBundle;

    /***
     * 初始化悬浮窗视图<P/>
     * 为了实现共享元素动效，初始视图布局宽度和高度填满整体视图，动效完成之后布局恢复至期望大小
     */
    public void initTopBarView(Activity activity){
        if(mTopBarAdded){
            return;
        }
        TopBarView topBarView = new TopBarView(getApplicationContext());;
        if(null == linearLayout){
            linearLayout = new LinearLayout(getApplicationContext());
            linearLayout.setClipChildren(false);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        lp.topMargin = 50;
        lp.leftMargin = 0;
        linearLayout.addView(topBarView, lp);

        WindowManager.LayoutParams winLp = new WindowManager.LayoutParams();
        winLp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        //使视图下层的布局可以响应触控事件
        winLp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        winLp.format = PixelFormat.RGBA_8888;
        winLp.width = 300;
        winLp.height = 400;
        winLp.token = new Binder();
        if(null == wm){
            wm = getSystemService(WindowManager.class);
        }
        wm.addView(linearLayout, winLp);
        mTopBarAdded = true;
//        topBarView.enterAnimation(mBundle);
//        ViewManager.getFloatView(activity);
    }

    public void goToTopIslandView(){
        mViewTransitionManager.setScene(R.layout.layout_view_top_island, linearLayout);
    }

    public void goToTopBarView(){
        mViewTransitionManager.setScene(R.layout.layout_view_top_bar, linearLayout);
    }

    public void setExitAnimationBundle(Bundle b){
        this.mBundle = b;
    }

    private void removeTopBarView(){
        if(null != linearLayout){
            if(linearLayout.isAttachedToWindow()){
                wm.removeView(linearLayout);
            }
            mTopBarAdded = false;
        }
    }


}
